import {
  getTypeList,
  addInfo,
  sendMessage
} from '../../utils/service.js'
import config from '../../config/config.js'
var app = getApp()

Page({
  data: {
    // text:"这是一个页面"
    array: ['默认分类'],
    index: 0,
    hidFlag: true,
    qrCode: '',
    url: '',
    mobileVal: '',
    timenum: 120,
    name: '获取验证码',
    timer:0,
  },

  /**
   * 监听普通picker选择器
   */
  listenerPickerSelected: function(e) {
    //改变index值，通过setData()方法重绘界面
    this.setData({
      index: e.detail.value
    });
  },

  /**
   * 监听时间picker选择器
   */
  listenerTimePickerSelected: function(e) {
    //调用setData()重新绘制
    this.setData({
      time: e.detail.value,
    });
  },

  /**
   * 监听日期picker选择器
   */
  listenerDatePickerSelected: function(e) {
    this.setDate({
      date: e.detail.value
    })
  },

  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.getTypeList();
  }, // 绑定上传图片的函数
  resetData: function(e) { // 这里是选取图片的方法
    var that = this
    that.setData({
      nameVal: '',
      typeName: '',
      ghId: '',
      labelVal: '',
      mobileVal: '',
      remarksVal: '',
      urlVal: '',
      qrCodeVal: '',
      qrCode: '',
      url: '', 
      codeVal: '',
      appidVal: '',
      appSecretVal: '',
    })

  },

  // 绑定上传图片的函数
  chooseImg: function(e) { // 这里是选取图片的方法
    var that = this
    debugger
    var typeT = e.currentTarget.dataset.typet
    wx.chooseImage({
      count: 1, // 最多可以选择9张图片，默认9
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var imgsrc = res.tempFilePaths
        that.uploadPhoto(imgsrc[0], typeT)
      }
    })
  }, // 在app.js中定义多图片上传函数 
  uploadPhoto: function(data, typeT) {
    var that = this
    var url = config.url.baseUrl + "/api/pc/upload/uploadImage"
    wx.uploadFile({
      url: url, //仅为示例
      filePath: data,
      name: 'file',
      formData: {
        'file': data,
      },
      success: function(res) {
        if (res.data) {
          if (typeT == 1) {
            that.setData({
              url: res.data.replace(/"/g, ''),
            })
          } else {
            that.setData({
              qrCode: res.data.replace(/"/g, ''),
            })
          }
          wx.showToast({
            icon: 'success',
            title: '上传成功!',
            duration: 3000
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: '上传失败!',
            duration: 3000
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          icon: 'none',
          title: '上传失败!',
          duration: 3000
        })
      }
    })
  },
  getCode: function() {
    var that = this;
    var mobile = that.data.mobileVal;

    if (that.checkMobile()) {

      sendMessage({
        mobile: mobile,
      }).then(res => {
        debugger
        if (res) {
          if (res.retCode == '000000') {
            wx.showToast({
              icon: 'none',
              title: '短信发送成功!',
              duration: 3000
            })
            var timer = setInterval(function() {
              debugger
              var timenum = that.data.timenum - 1
              that.setData({
                timenum: timenum,
                name: '已发送',
              })
              if (timenum <= 0) {
                that.setData({
                  timenum: 120,
                  name: '获取验证码',
                })
                clearInterval(that.data.timer)
              }
            }, 1000)
            that.setData({
              timer: timer
            })

          } else {
            wx.showToast({
              icon: 'none',
              title: '短信发送失败!',
              duration: 3000
            })
          }
        }
      }).catch(e => {
        console.log(e);
        wx.showToast({
          icon: 'none',
          title: '短信发送失败!',
          duration: 3000
        })
      })
    }
  },
  mobileInput: function(e) {
    var inputValue = e.detail.value;
    inputValue = inputValue.replace(/(^\s*)|(\s*$)/g, "");
    this.setData({
      mobileVal: inputValue
    })
  },
  checkMobile: function() {
    var that = this
    var mobile = that.data.mobileVal;
    if (mobile.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号码!',
        duration: 3000
      })
      return false
    }
    if (isNaN(mobile)) {
      wx.showToast({
        icon: 'none',
        title: '手机号码错误!',
        duration: 3000
      });
      return false
    }

    if (mobile.length != 11) {
      wx.showToast({
        icon: 'none',
        title: '请输入11位手机号码!',
        duration: 3000
      })
      return false
    }
    return true
  },
  addInfo: function(e) {
    var that = this;
    debugger
    var name = e.detail.value.name;
    if (!name || name == null || name == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小程序名称!',
        duration: 3000
      })
      return
    }
    

    var ghId = e.detail.value.ghId;
    if (!ghId || ghId == null || ghId == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小程序原始ID',
        duration: 3000
      })
      return
    }

    var appidVal = e.detail.value.appid;
    if (!appidVal || appidVal == null || appidVal == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小程序appid!',
        duration: 3000
      })
      return
    }

    var appSecretVal = e.detail.value.appSecret;
    if (!appSecretVal || appSecretVal == null || appSecretVal == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小程序appSecret!',
        duration: 3000
      })
      return
    }

    var typeName = e.detail.value.typeName;
    var label = e.detail.value.label;
    if (!label || label == null || label == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小程序标签!',
        duration: 3000
      })
      return
    }

    var remarks = e.detail.value.remarks; 
    if (!remarks || remarks == null || remarks == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小程序描述!',
        duration: 3000
      })
      return
    }


    if (!that.checkMobile()) {
      return
    }
    var mobile = that.data.mobileVal;
    debugger
    var code = e.detail.value.code;
    if (!code || code == null || code == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入验证码!',
        duration: 3000
      })
      return
    }
    var url = e.detail.value.url;
    if (!url || url == null || url == '') {
      wx.showToast({
        icon: 'none',
        title: '请上传小程序图标!',
        duration: 3000
      })
      return
    }
    var qrCode = e.detail.value.qrCode;
    if (!qrCode || qrCode == null || qrCode == '') {
      wx.showToast({
        icon: 'none',
        title: '请上传小程序二维码!',
        duration: 3000
      })
      return
    }

    that.setData({
      hidFlag: false,
    });
    debugger

    addInfo({
      name: name,
      type: typeName,
      label: label,
      mobile: mobile,
      qrCode: qrCode,
      url: url,
      remarks: remarks,
      code: code,
      appid: appidVal,
      ghId:ghId,
      appSecret: appSecretVal
    }).then(res => {
      debugger
      that.setData({
        hidFlag: true,
      });

      if (res.retCode == '000000') {
        that.setData({
          timenum: 120,
          name: '获取验证码',
        })
        clearInterval(that.data.timer)
        that.setData({
          hidFlag: true,
        });
        that.resetData();
        wx.showToast({
          icon: 'success',
          title: '保存成功!',
          duration: 3000
        });
      } else {
        wx.showToast({
          icon: 'none',
          title: res.retMsg,
          duration: 3000
        })
      }
    }).catch(e => {
      that.setData({
        hidFlag: true,
      });
      wx.showToast({
        icon: 'success',
        title: '保存失败!',
        duration: 3000
      });
    })
  },

  getTypeList: function() {
    var that = this;
    getTypeList({}).then(res => {
      if (res && res.length > 0) {
        that.setData({
          array: res,
        });
      }
    }).catch(e => {

    })
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },

})